/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.expert.dao.ExpertManualServiceDao;
import com.chlitina.store.modules.expert.entity.ExpertManualService;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 专家人工分析Service
 * @author lvbo
 * @version 2013-07-09
 */
@Component
@Transactional(readOnly = true)
public class ExpertManualServiceService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ExpertManualServiceService.class);
	
	@Autowired
	private ExpertManualServiceDao expertManualServiceDao;
	
	public ExpertManualService get(Long id) {
		return expertManualServiceDao.findOne(id);
	}
	
	public Page<ExpertManualService> find(Page<ExpertManualService> page, ExpertManualService expertManualService ,String status, String startDate, String endDate) {
		page.setPageSize(Integer.parseInt(Global.getConfig("page.longPageSize")));
		expertManualServiceDao.clear();
		DetachedCriteria dc = expertManualServiceDao.createDetachedCriteria();
		if (expertManualService.getStore()!=null && expertManualService.getStore().getId()>0){
			dc.add(Restrictions.eq("store.id", expertManualService.getStore().getId()));
		}
		int statusInt = 0;
		Date start = null;
		Date end = null;
		try {
			if (status != null) {
				statusInt = Integer.parseInt(status);
			}
			start = DateUtils.parseDate(startDate);
			start = DateUtils.getBeginOfDay(start);
			end = DateUtils.parseDate(endDate);
			end = DateUtils.getEndOfDay(end);
		} catch (Exception e) {
//			System.out.println("字符串转换日期异常");
		}
		if (start != null && end == null) {
			dc.add(Restrictions.ge("askDatetime", start));
		} else if (start == null && end != null) {
			dc.add(Restrictions.le("askDatetime", end));
		} else if (start != null && end != null) {
			dc.add(Restrictions.between("askDatetime", start, end));
		}
		if (statusInt != -1) {
			dc.add(Restrictions.eq("status", statusInt));
		}
		dc.addOrder(Order.desc("id"));
		return expertManualServiceDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public int getExpertManualAmount() {
		DetachedCriteria dc = expertManualServiceDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store.id", StoreUtils.getStore().getId()));
		dc.add(Restrictions.between("askDatetime", DateUtils.getFirstDay(), DateUtils.getLastDay()));
		List<ExpertManualService> lists = expertManualServiceDao.find(dc);
		return lists == null ? 0 : lists.size(); 
	}
	@Transactional(readOnly = false)
	public void save(ExpertManualService expertManualService) {
		expertManualServiceDao.save(expertManualService);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		expertManualServiceDao.delete(id);
	}
	
}
